package www.openteam.com.adapter.realtime;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.EvaAMbean;
import www.openteam.com.bean.PbMaterial;
import www.openteam.com.tool.AdpUtil;

public class EvaAMadp extends RecyclerView.Adapter<EvaAMadp.ViewHolder> {

    Context context;
    List<EvaAMbean> beanList;
    List<PbMaterial.DataBeanX.DataBean> aMList;
    ArrayAdapter<String> aMNameAdp;
    List<String> numbList;


    public EvaAMadp(Context context) {
        this.context = context;
        beanList = new ArrayList<>();
        EvaAMbean evaAMbean = new EvaAMbean("0", "0", "0", "0", "0", "0", "0",
                "0", "0", "0");
        beanList.add(evaAMbean);
    }

    public List<EvaAMbean> getBeanList() {
        return beanList;
    }

    public void setNumbList(List<String> numbList) {
        this.numbList = numbList;

    }

    public void setaMList(List<PbMaterial.DataBeanX.DataBean> list) {
        this.aMList = aMList;
        List<String> nameList = new ArrayList<>();
        for (PbMaterial.DataBeanX.DataBean dataBean : list) {
            nameList.add(dataBean.getName());
        }
        aMNameAdp = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_dropdown_item, nameList);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_aux_material, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


        EvaAMbean evaAMbean = beanList.get(position);
        EvaAMbean bean = new EvaAMbean();
        EvaAMbean baseBean = new EvaAMbean("0", "0", "0", "0", "0", "0", "0",
                "0", "0", "0");
//        AdpUtil.setEditTextWatcher(holder.evaluateAMCode,data -> bean.seteCode(data.toString()),
//                ()->bean.geteCode());
        AdpUtil.setEditTextWatcher(holder.evaluateAMExtraAdd, data -> bean.seteExtraAdd(data.toString()),
                () -> bean.geteExtraAdd());
//        AdpUtil.setEditTextWatcher(holder.evaluateAMName,data -> bean.seteName(data.toString()),
//                ()->bean.geteName());
//        AdpUtil.setEditTextWatcher(holder.evaluateAMPart,data -> bean.setePart(data.toString()),
//                ()->bean.getePart());
//        AdpUtil.setEditTextWatcher(holder.evaluateAMPickNumb,data -> bean.setePickNumb(data.toString()),
//                ()->bean.getePickNumb());
        AdpUtil.setEditTextWatcher(holder.evaluateAMPrice, data -> bean.setePrice(data.toString()),
                () -> bean.getePrice());
        AdpUtil.setEditTextWatcher(holder.evaluateAMRemark, data -> bean.seteRemark(data.toString()),
                () -> bean.geteRemark());
//        AdpUtil.setEditTextWatcher(holder.evaluateAMSpecs,data -> bean.seteSpecs(data.toString()),
//                ()->bean.geteSpecs());
//        AdpUtil.setEditTextWatcher(holder.evaluateAMUnit,data -> bean.seteUnit(data.toString()),
//                ()->bean.geteUnit());
        AdpUtil.setEditTextWatcher(holder.evaluateAMUnitCost, data -> bean.seteUnitCost(data.toString()),
                () -> bean.geteUnitCost());

        holder.evaluateAMPickNumb.setText(numbList.get(0));
        AdpUtil.setSpinnerAdp(holder.evaluateAMSpMonery,numbList,context);
        holder.evaluateAMSpMonery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String numb = numbList.get(i);//根据数量来计算，对后面的具体金额进行赋值，databean也要设值
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        if (aMList != null) {
            holder.evaluateAMName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    PbMaterial.DataBeanX.DataBean dataBean = aMList.get(i);
                    holder.evaluateAMCode.setText(dataBean.getCode());
                    holder.evaluateAMSpecs.setText(dataBean.getSpecs());
                    holder.evaluateAMUnit.setText(dataBean.getUnit());
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }

        if (position == beanList.size() - 1) {
            holder.evaluateAMAdd.setVisibility(View.VISIBLE);
        } else {
            holder.evaluateAMAdd.setVisibility(View.GONE);
        }


        holder.evaluateAMAdd.setOnClickListener(v -> {
            if (beanList.size() <= 10) {
                //最多只能新增10个
                beanList.add(position + 1, baseBean);
                notifyDataSetChanged();
                notifyItemRemoved(position + 1);
            }
        });

        holder.evaluateAMDelete.setOnClickListener(v -> {
            if (beanList.size() > 1) {
                //留一个不删除
                beanList.remove(position);
                notifyDataSetChanged();
                notifyItemRemoved(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.evaluate_AM_part)
        AppCompatSpinner evaluateAMPart;
        @Bind(R.id.evaluate_AM_code)
        TextView evaluateAMCode;
        @Bind(R.id.evaluate_AM_name)
        AppCompatSpinner evaluateAMName;
        @Bind(R.id.evaluate_AM_specs)
        TextView evaluateAMSpecs;
        @Bind(R.id.evaluate_AM_unit)
        TextView evaluateAMUnit;
        @Bind(R.id.evaluate_AM_unitCost)
        EditText evaluateAMUnitCost;
        @Bind(R.id.evaluate_AM_extraAdd)
        EditText evaluateAMExtraAdd;
        @Bind(R.id.evaluate_AM_pickNumb)
        TextView evaluateAMPickNumb;
        @Bind(R.id.evaluate_AM_price)
        EditText evaluateAMPrice;
        @Bind(R.id.evaluate_AM_remark)
        EditText evaluateAMRemark;
        @Bind(R.id.evaluate_AM_update)
        Button evaluateAMUpdate;
        @Bind(R.id.evaluate_AM_delete)
        Button evaluateAMDelete;
        @Bind(R.id.evaluate_AM_add)
        Button evaluateAMAdd;
        @Bind(R.id.evaluate_AM_SpMonery)
        AppCompatSpinner evaluateAMSpMonery;
        @Bind(R.id.evaluate_AM_EdMonery)
        EditText evaluateAMEdMonery;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
